/**
 * 
 */
package vanhoc.android.medialcool.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

/**
 * @author SEVEN
 *
 */
public class ImageHelper {

	private Context _context;
	private String[] arrExtension = new String[] {"jpg","JPG","png","PNG","gif","GIF","jpeg", "JPEG"};
	
	
	
	public ImageHelper() {
	}
	
	
	public ImageHelper(Context ct) {
		this._context = ct;
	}
	
	
	public Bitmap getBitMap(InputStream is){
		Bitmap bm =null;
		try {
			BitmapFactory.Options bmOptions = new BitmapFactory.Options();
			bm = BitmapFactory.decodeStream(is, null, bmOptions);
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bm;
	}
	
	
	public Boolean storeImage(String extStorageDirectory, String fileName, Bitmap bm){
		Boolean success = null;
		OutputStream outStream = null;
		File file = new File(extStorageDirectory, fileName);
		
		try {
			outStream = new FileOutputStream(file);
			success = bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
			outStream.flush();
			outStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(_context, e.toString(), Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(_context, e.toString(), Toast.LENGTH_LONG).show();
		}
		
		return success;
	}
	
	
	
	/**
	 * Kiem tra xem 1 file co phai la file hinh anh hay khong
	 * @param fileString
	 * @return
	 */
	public Boolean isImageFile(File file){
		String str[] = file.toString().split("\\.");
		String extensionFile = str[str.length-1];
		
		for (int i = 0; i < arrExtension.length; i++) {
			if (arrExtension[i].equals(extensionFile)) {
				return true;
			}
		}
		return false;
	}
	
	
	/**
	 * Lay ve ten file (bo phan extension cua file)
	 * @param imgFile
	 * @return
	 */
	public String getImageName(File imgFile) {
		String imgName = null;
		
		String str[] = imgFile.toString().split("\\.");
		if (str.length == 2) { //vd : tenhinh.jpg
			imgName = str[0];
		}else if (str.length > 2) { //vidu : tunhien.tenhinh.thumb.jpg
			for (int i = 0; i < str.length-1; i++) {
				if (i == 0) {
					imgName = str[0];
				}else{
					imgName = imgName + "." + str[i];
				}
			}
		}
		return imgName;
	}
	
	
	public String getImageName(String imgFile) {
		String imgName = null;
		
		String str[] = imgFile.split("\\.");
		if (str.length == 2) { //vd : tenhinh.jpg
			imgName = str[0];
		}else if (str.length > 2) { //vidu : tunhien.tenhinh.thumb.jpg
			for (int i = 0; i < str.length-1; i++) {
				if (i == 0) {
					imgName = str[0];
				}else{
					imgName = imgName + "." + str[i];
				}
			}
		}
		return imgName;
	}
}
